package service

import (
	"context"
	v1 "gitee.com/jacktuotuo/coolcar/api/trip/service/v1"
	"gitee.com/jacktuotuo/coolcar/app/trip/internal/biz"
	"gitee.com/jacktuotuo/coolcar/pkg/auth"
)

func (t *TripService) CreateTrip(ctx context.Context, in *v1.CreateTripRequest) (*v1.CreateTripReply, error) {

	accountId, err := auth.AccountIdFromContext(ctx)

	aid := accountId.String()

	t.log.Errorf("accountId : %s", aid)

	l := &biz.Location{
		Latitude:  in.Start.Latitude,
		Longitude: in.Start.Longitude,
	}

	ls := &biz.LocationStatus{
		Location: l,
	}

	trip := &biz.Trip{
		Start:     ls,
		Current:   ls,
		AccountId: aid,
		CarId:     in.CarId,
		Status:    biz.TripStatus_IN_PROGRESS,
	}

	trip, err = t.uc.Create(ctx, trip)
	if err != nil {
		return nil, err
	}

	reply := &v1.CreateTripReply{
		Entity: &v1.TripEntity{
			Id: trip.Id,
			Trip: &v1.Trips{
				AccountId: trip.AccountId,
				CarId:     trip.CarId,
				Start: &v1.LocationStatus{
					Location: &v1.Location{
						Latitude:  trip.Start.Location.Latitude,
						Longitude: trip.Start.Location.Longitude,
					},
					PoiName: trip.Start.PoiName,
				},
				Status:     v1.TripStatus(trip.Status),
				IdentityId: trip.IdentityId,
			},
		},
	}

	return reply, nil
}

func (t *TripService) GetTrip(ctx context.Context, in *v1.GetTripRequest) (*v1.GetTripReply, error) {
	panic("implement me")
}

func (t *TripService) GetTrips(ctx context.Context, in *v1.GetTripRequest) (*v1.GetTripReply, error) {
	panic("implement me")
}

func (t *TripService) UpdateTrip(ctx context.Context, in *v1.UpdateTripRequest) (*v1.UpdateTripReply, error) {
	panic("implement me")
}
